import { Component, OnInit } from '@angular/core';
import {DicsService} from "../../shared/service/dics.service";
import {dicType} from "../../constant/dictionary";
import {QINIU_DOMAIN} from "../../constant/config";
import {GoodsService} from "../../shared/service/goods.service";
import {fadeInAnimation} from "../../routing/route.animation";
import {myValidNull} from "../../shared/util/string-utils";
import {Router} from "@angular/router";

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss'],
  host: {
    '[@fadeInAnimation]': 'true'
  },
  animations: [ fadeInAnimation ],
  providers:[
    GoodsService
  ]
})
export class HomeComponent implements OnInit {
  qiniuDomain=QINIU_DOMAIN;
  goodTypes:any[]=[];
  currType:any={};
  currTypeIndex:number=0;

  PAN_ACTION = {  UP: 'panup', DOWN: 'pandown',LEFT: 'panleft', RIGHT: 'panright' };
  SWIPE_ACTION = {  UP: 'swipeup', DOWN: 'swipedown',LEFT: 'swipeleft', RIGHT: 'swiperight' };
  bannerIndex:any=0;

  goodsList:any[]=[];
  page:any={pageNo:1,pageSize:6,totalCount: -1};
  constructor(
    private dicsService:DicsService,
    private goodsService:GoodsService,
    private router:Router,
    ) { }

  ngOnInit() {
    this.initDics();
  }
  /**
   * 初始化字典数据
   */
  initDics(){
    this.dicsService.getDics(dicType.goodsType).then((data)=>{
      console.log(data)
      this.goodTypes = data.GOODS_TYPE;
      this.currType = this.goodTypes[0];
      this.searchGoods(null);
    },(error)=>{
      console.log("获取 goodsType 字典出错：",error);
    })
  }

  searchGoods(e){
    this.goodsService.goodsList(this.page,null,this.currType.value).then((goods)=>{
      // console.log(goods);
      this.goodsList = goods;
    },error=>console.error("获取商品出错：",error))
  }
  goGoodsDetail(e,id){
    this.router.navigate(["auth-guard/goods-detail/",id]);
  }
  selectBanner(e){
    // console.log(e)
    let nextIndex = 0;
    if( e.type === this.SWIPE_ACTION.RIGHT) {
      const isFirst = this.bannerIndex === 0;
      nextIndex = isFirst ? 3 : this.bannerIndex - 1;
    }
    if( e.type === this.SWIPE_ACTION.LEFT) {
      const isLast = this.bannerIndex === 3;
      nextIndex = isLast ? 0 : this.bannerIndex + 1;
    }

    // console.log(bannerIndex);
    this.bannerIndex = nextIndex;
  }
  selectType(e){
    // console.log(e)
    let nextIndex = 0;
    if( e.type === this.SWIPE_ACTION.RIGHT) {
      const isFirst = this.currTypeIndex === 0;
      // nextIndex = isFirst ? this.areaData.length - 1 : this.provinceNum - 1;
      nextIndex = isFirst ? 0: this.currTypeIndex - 1;
    }
    if( e.type === this.SWIPE_ACTION.LEFT) {
      const isLast = this.currTypeIndex === this.goodTypes.length - 1;
      nextIndex = isLast ? this.goodTypes.length - 1 : this.currTypeIndex + 1;
      // nextIndex = isLast ? 1 : this.provinceNum + 1;
    }

    // console.log(nextIndex);
    this.currTypeIndex = nextIndex;
    let type = this.goodTypes[this.currTypeIndex];
    if(this.currType.value != type.value){
      this.currType = this.goodTypes[this.currTypeIndex];
      this.searchGoods(null);
    }
  }
  isExist(imgKey){
    return !myValidNull(imgKey);
  }

}
